﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgvDemo
{
    public class TrafficDetailClass
    {
        public int TrafficAreaNum { get; set; }

        public int[] TrafficInArray { get; set; }

        public int[] TrafficWaitArray { get; set; }

        public int TrafficRole { get; set; }
    }

    public class TrafficManagerClass
    {
        private static TrafficManagerClass uniqueInstance;

        private TrafficManagerClass()
        {
            TrafficDetailDic = new Dictionary<int, TrafficDetailClass>();
            for (int i = 1; i < 30; i++)
            {
                TrafficDetailClass tempClass = new TrafficDetailClass();
                tempClass.TrafficAreaNum = i;
                tempClass.TrafficInArray = new int[3] { 0, 0, 0 };
                tempClass.TrafficWaitArray = new int[5] { 0, 0, 0, 0, 0 };
                tempClass.TrafficRole = 0;
                TrafficDetailDic.Add(i, tempClass);
            }
        }

        public static Dictionary<int, TrafficDetailClass> TrafficDetailDic { get; set; }
        public static object lockTraffic = new object();//防止下位机数据处理异常

        public static object lockDoor = new object();//防止下位机数据处理异常

        public static object lockSpecialOk = new object();//防止下位机数据处理异常

        public static object lockSpecial = new object();//防止下位机数据处理异常

        public static TrafficManagerClass GetInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new TrafficManagerClass();
            }

            return uniqueInstance;
        }


        /// <summary>
        /// 交管复位处理
        /// </summary>
        /// <param name="num"></param>
        public static void ClearTrafficArea(int num)
        {
            try
            {
                //lock (TrafficDetailDic)
                {
                    foreach (int area in TrafficDetailDic.Keys)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            if (TrafficDetailDic[area].TrafficInArray[i] == num)
                            {
                                if (i == 0)
                                {
                                    if (TrafficDetailDic[area].TrafficInArray[1] != 0)
                                    {
                                        TrafficDetailDic[area].TrafficInArray[0] = TrafficDetailDic[area].TrafficInArray[1];
                                        TrafficDetailDic[area].TrafficInArray[1] = TrafficDetailDic[area].TrafficInArray[2];
                                        TrafficDetailDic[area].TrafficInArray[2] = 0;
                                    }
                                    else
                                    {
                                        if (TrafficDetailDic[area].TrafficInArray[1] != 0)
                                        {
                                            TrafficDetailDic[area].TrafficInArray[0] = TrafficDetailDic[area].TrafficInArray[1];
                                            TrafficDetailDic[area].TrafficInArray[1] = TrafficDetailDic[area].TrafficInArray[2];
                                            TrafficDetailDic[area].TrafficInArray[2] = 0;
                                        }
                                        else
                                        {
                                            if (TrafficDetailDic[area].TrafficWaitArray[0] == 0)
                                            {
                                                TrafficDetailDic[area].TrafficInArray[0] = 0;
                                            }
                                            else
                                            {
                                                AgvFunctionClass.TrafficDic[TrafficDetailDic[area].TrafficWaitArray[0]] = 2;
                                                TrafficDetailDic[area].TrafficInArray[0] = TrafficDetailDic[area].TrafficWaitArray[0];
                                                TrafficDetailDic[area].TrafficWaitArray[0] = TrafficDetailDic[area].TrafficWaitArray[1];
                                                TrafficDetailDic[area].TrafficWaitArray[1] = TrafficDetailDic[area].TrafficWaitArray[2];
                                                TrafficDetailDic[area].TrafficWaitArray[2] = TrafficDetailDic[area].TrafficWaitArray[3];
                                                TrafficDetailDic[area].TrafficWaitArray[3] = TrafficDetailDic[area].TrafficWaitArray[4];
                                                TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                            }
                                        }
                                    }
                                }
                                else if (i == 1)
                                {
                                    TrafficDetailDic[area].TrafficInArray[1] = TrafficDetailDic[area].TrafficInArray[2];
                                    TrafficDetailDic[area].TrafficInArray[2] = 0;
                                }
                                else if (i == 2)
                                {
                                    TrafficDetailDic[area].TrafficInArray[2] = 0;
                                }
                            }
                        }

                        for (int i = 0; i < 5; i++)
                        {
                            if (TrafficDetailDic[area].TrafficWaitArray[i] == num)
                            {
                                if (i == 0)
                                {
                                    TrafficDetailDic[area].TrafficWaitArray[0] = TrafficDetailDic[area].TrafficWaitArray[1];
                                    TrafficDetailDic[area].TrafficWaitArray[1] = TrafficDetailDic[area].TrafficWaitArray[2];
                                    TrafficDetailDic[area].TrafficWaitArray[2] = TrafficDetailDic[area].TrafficWaitArray[3];
                                    TrafficDetailDic[area].TrafficWaitArray[3] = TrafficDetailDic[area].TrafficWaitArray[4];
                                    TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                }
                                else if (i == 1)
                                {
                                    TrafficDetailDic[area].TrafficWaitArray[1] = TrafficDetailDic[area].TrafficWaitArray[2];
                                    TrafficDetailDic[area].TrafficWaitArray[2] = TrafficDetailDic[area].TrafficWaitArray[3];
                                    TrafficDetailDic[area].TrafficWaitArray[3] = TrafficDetailDic[area].TrafficWaitArray[4];
                                    TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                }
                                else if (i == 2)
                                {
                                    TrafficDetailDic[area].TrafficWaitArray[2] = TrafficDetailDic[area].TrafficWaitArray[3];
                                    TrafficDetailDic[area].TrafficWaitArray[3] = TrafficDetailDic[area].TrafficWaitArray[4];
                                    TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                }
                                else if (i == 3)
                                {
                                    TrafficDetailDic[area].TrafficWaitArray[3] = TrafficDetailDic[area].TrafficWaitArray[4];
                                    TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                }
                                else if (i == 4)
                                {
                                    TrafficDetailDic[area].TrafficWaitArray[4] = 0;
                                }
                            }
                        }
                    }

                    //for (int i = 0; i < TrafficDataList.Count; i++)
                    //{
                    //    for (int j = 0; j < 3; j++)
                    //    {
                    //        if (TrafficDataList[i].TrafficInArray[j] == num)
                    //        {
                    //            if (j == 0)
                    //            {
                    //                if (TrafficDataList[i].TrafficInArray[1] != 0)
                    //                {
                    //                    TrafficDataList[i].TrafficInArray[0] = TrafficDataList[i].TrafficInArray[1];
                    //                    TrafficDataList[i].TrafficInArray[1] = TrafficDataList[i].TrafficInArray[2];
                    //                    TrafficDataList[i].TrafficInArray[2] = 0;
                    //                }
                    //                else
                    //                {

                    //                    if (TrafficDataList[i].TrafficWaitArray[0] == 0)
                    //                        {
                    //                            TrafficDataList[i].TrafficInArray[0] = 0;
                    //                        }
                    //                        else
                    //                        {
                    //                            agvF.TrafficDic[TrafficDataList[i].TrafficWaitArray[0]] = 2;
                    //                            TrafficDataList[i].TrafficInArray[0] = TrafficDataList[i].TrafficWaitArray[0];
                    //                            TrafficDataList[i].TrafficWaitArray[0] = TrafficDataList[i].TrafficWaitArray[1];
                    //                            TrafficDataList[i].TrafficWaitArray[1] = TrafficDataList[i].TrafficWaitArray[2];
                    //                            TrafficDataList[i].TrafficWaitArray[2] = TrafficDataList[i].TrafficWaitArray[3];
                    //                            TrafficDataList[i].TrafficWaitArray[3] = TrafficDataList[i].TrafficWaitArray[4];
                    //                            TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //                        }

                    //                }
                    //            }
                    //            else if (i == 1)
                    //            {
                    //                TrafficDataList[i].TrafficInArray[1] = TrafficDataList[i].TrafficInArray[2];
                    //                TrafficDataList[i].TrafficInArray[2] = 0;
                    //            }
                    //            else if (i == 2)
                    //            {
                    //                TrafficDataList[i].TrafficInArray[2] = 0;
                    //            }
                    //        }
                    //    }


                    //    for (int k = 0; k < 5; i++)
                    //    {
                    //        if (TrafficDataList[i].TrafficWaitArray[k] == num)
                    //        {
                    //            if (k == 0)
                    //            {
                    //                TrafficDataList[i].TrafficWaitArray[0] = TrafficDataList[i].TrafficWaitArray[1];
                    //                TrafficDataList[i].TrafficWaitArray[1] = TrafficDataList[i].TrafficWaitArray[2];
                    //                TrafficDataList[i].TrafficWaitArray[2] = TrafficDataList[i].TrafficWaitArray[3];
                    //                TrafficDataList[i].TrafficWaitArray[3] = TrafficDataList[i].TrafficWaitArray[4];
                    //                TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //            }
                    //            else if (k == 1)
                    //            {
                    //                TrafficDataList[i].TrafficWaitArray[1] = TrafficDataList[i].TrafficWaitArray[2];
                    //                TrafficDataList[i].TrafficWaitArray[2] = TrafficDataList[i].TrafficWaitArray[3];
                    //                TrafficDataList[i].TrafficWaitArray[3] = TrafficDataList[i].TrafficWaitArray[4];
                    //                TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //            }
                    //            else if (k == 2)
                    //            {
                    //                TrafficDataList[i].TrafficWaitArray[2] = TrafficDataList[i].TrafficWaitArray[3];
                    //                TrafficDataList[i].TrafficWaitArray[3] = TrafficDataList[i].TrafficWaitArray[4];
                    //                TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //            }
                    //            else if (k == 3)
                    //            {
                    //                TrafficDataList[i].TrafficWaitArray[3] = TrafficDataList[i].TrafficWaitArray[4];
                    //                TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //            }
                    //            else if (k == 4)
                    //            {
                    //                TrafficDataList[i].TrafficWaitArray[4] = 0;
                    //            }
                    //        }
                    //    }
                    //}

                }

            }
            catch
            {

            }

        }





        #region 交管功能
        /// <summary>
        /// 实时交管
        /// </summary>
        /// <param name="agvInfo"></param>
        /// <param name="agvF"></param>
        /// <param name="dataBase"></param>
        /// <param name="mainManager"></param>


        public void TrafficSpecial(PlcManagerClass pmc)
        {
            try
            {
                if (pmc.chargeState9.CurrentStation == 200 && pmc.chargeState9.AgvState == 3)
                {
                    pmc.UnLockAgv();
                    ClearTrafficArea(pmc.chargeState9.AgvNum);
                }
            }
            catch
            { }

        }

        public void TrafficHandle(PlcManagerClass pmc)
        {
            try
            {
                lock (lockTraffic)
                {

                    if (pmc.chargeState9.CurrentStation != 0)
                    {
                        if (AgvFunctionClass.TrafficSureDic[1] == 0)
                        { 
                            if (pmc.chargeState9.CurrentStation == 12 && pmc.chargeState9.AgvState == 0)
                            {
                                AgvFunctionClass.TrafficSureDic[1] = pmc.chargeState9.AgvNum;
                            } 
                        }
                        else
                        {
                            if (AgvFunctionClass.TrafficSureDic[1] == pmc.chargeState9.AgvNum)
                            {
                                if (pmc.chargeState9.CurrentStation == 12 && pmc.chargeState9.AgvState == 0)
                                {
                                    pmc.StartAgv();
                                }
                                else if (pmc.chargeState9.CurrentStation != 12 && pmc.chargeState9.CurrentStation != 141 && pmc.chargeState9.CurrentStation != 142)
                                {
                                   
                                    AgvFunctionClass.TrafficSureDic[1] = 0;
                                  
                                }
                            }
                        }


                    }


                }
            }
            catch (Exception ex)
            {
            }

        }



        public void TrafficHandleDoor(PlcManagerClass pmc)
        {
            try
            {
                lock (lockDoor)
                {

                    if (pmc.chargeState9.CurrentStation != 0)
                    {
                        if (AgvFunctionClass.TrafficSureDic[2] == 0) 
                        {
                            if (pmc.chargeState9.CurrentStation == 11 && pmc.chargeState9.AgvState == 0)
                            {
                                AgvFunctionClass.TrafficSureDic[2] = pmc.chargeState9.AgvNum;
                            }
                        }
                        else
                        {
                            if (AgvFunctionClass.TrafficSureDic[2] == pmc.chargeState9.AgvNum)
                            {
                                if (pmc.chargeState9.CurrentStation == 11 && pmc.chargeState9.AgvState == 0)
                                {
                                    pmc.StartAgv();
                                }
                                else if (pmc.chargeState9.CurrentStation != 11)
                                {

                                    AgvFunctionClass.TrafficSureDic[2] = 0;

                                }
                            }
                        }


                    }


                }
            }
            catch (Exception ex)
            {
            }

        }




        private bool IsExistCross(string str, int value)
        {
            bool b = false;
            try
            {
                string[] strArray = str.Split(',');

                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i] == value.ToString())
                    {
                        b = true;
                    }
                }

                return b;
            }
            catch
            { return b; }
        }

      

        public void TrafficSure(PlcManagerClass cpc)
        {

            try
            {
               
                    int n = 0;
                    if (cpc.chargeState9.AgvState == 3)
                    {
                        foreach (int areaNum in TrafficDetailDic.Keys)
                        {
                            if (TrafficDetailDic[areaNum].TrafficWaitArray.Contains(cpc.chargeState9.AgvNum) || TrafficDetailDic[areaNum].TrafficInArray.Contains(cpc.chargeState9.AgvNum))
                            {
                                n++;
                                break;
                            }
                        }


                        if (n == 0)
                        {
                            AgvFunctionClass.TrafficDic[cpc.chargeState9.AgvNum] = 2;
                        }
                    }
              
            }
            catch
            {
            }

        }
        #endregion

        #region 交管逻辑判断
        private bool JudgeTrafficIn(int id)
        {
            bool isEx = false;
            foreach (int area in TrafficDetailDic.Keys)
            {
                if (area == id)
                {
                    if (JudgeArray(TrafficDetailDic[area].TrafficInArray) == true || JudgeArray(TrafficDetailDic[area].TrafficWaitArray) == true)
                    {
                        isEx = true;
                        break;
                    }
                }
            }


            return isEx;
        }

        private bool JudgeArray(int[] array)
        {
            bool isEx = false;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != 0)
                {
                    isEx = true;
                    break;
                }
            }
            return isEx;
        }

        private bool JudgeTrafficIsEx(int num)
        {
            bool isEx = false;
            foreach (int area in TrafficDetailDic.Keys)
            {

                if (JudgeArrayIsEx(TrafficDetailDic[area].TrafficInArray, num) == true || JudgeArrayIsEx(TrafficDetailDic[area].TrafficWaitArray, num) == true)
                {
                    isEx = true;
                    break;
                }

            }


            return isEx;
        }
        private bool JudgeArrayIsEx(int[] array, int num)
        {
            bool isEx = false;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == num)
                {
                    isEx = true;
                    break;
                }
            }

            return isEx;
        }
        #endregion
    }
}
